a=int(input())
maximum=-float('inf')
l=a//10
l1=a//100
l2=a//1000
l3=a%10
l4=a%100
l5=a//10%100
l6=a

m=[x for x in range(0,(a+a))]
for i in range(2,len(m)):
    for j in range(i+i,len(m),i):
        if m[i]!=0 and m[j]%m[i]==0:
            m[j]=0
m=sorted(list(set(m))[1:])
if  a<=1000:
    print(0)
elif int(str(l3*4))==a:
    print(0)
else:
    p=len(m)
    for n in range(1,p):
        if l6==m[n]:
           maximum=max(l6,maximum)
        else:
            pass
        if l==m[n]:
            maximum=max(l,maximum)
        else:
            pass
        if l1==m[n]:
            maximum=max(l1,maximum)
        else:
            pass
        if l2==m[n]:
            maximum=max(l2,maximum)
        else:
            pass
        if l3==m[n]:
            maximum=max(l3,maximum)
        else:
            pass
        if l4==m[n]:
            maximum=max(l4,maximum)
        else:
            pass
        if l5==m[n]:
            maximum=max(l5,maximum)
        else:
            pass
    if maximum<0:
        print(0)
    else:
        print(maximum)
